package com.fourth.fronted.service.impl;/**
 * Created by Administrator on 2019/6/21.
 */



import com.fourth.entity.Orders;
import com.fourth.fronted.service.OrdersService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.AbstractJavaTypeMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * RabbitMQ通用的消息发送服务
 * @Author:debug (SteadyJack)
 * @Date: 2019/6/21 21:47
 **/
@Service
public class RabbitSenderService {

    public static final Logger log= LoggerFactory.getLogger(RabbitSenderService.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private Environment env;

    @Autowired
    private OrdersService ordersService;


    /**
     * 确认订单成功后生成订单-发送信息入死信队列，等待着一定时间失效超时未支付的订单
     * @param oNo
     */
    public void sendKillSuccessOrderExpireMsg(final String oNo){
        try {
            if (StringUtils.isNotBlank(oNo)){
                //查询oNo订单信息
                List<Orders> orders = ordersService.selectOrdersByONo(oNo);
                System.out.println("消息队列orders"+orders);
                if (orders!=null){
                    rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
                    //普通交换机
                    rabbitTemplate.setExchange(env.getProperty("mq.kill.item.success.kill.dead.prod.exchange"));
                    //创建基本交换机+基本路由
                    rabbitTemplate.setRoutingKey(env.getProperty("mq.kill.item.success.kill.dead.prod.routing.key"));
                    //发送消息
                    rabbitTemplate.convertAndSend(orders, new MessagePostProcessor() {
                        @Override
                        public Message postProcessMessage(Message message) throws AmqpException {
                            MessageProperties mp=message.getMessageProperties();
                            //PERSISTENT 持久
                            mp.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                            mp.setHeader(AbstractJavaTypeMapper.DEFAULT_CONTENT_CLASSID_FIELD_NAME, Orders.class);
                            System.out.println("消息队列准备发送消息");
                            //设置过期时间
                            //TODO：动态设置TTL(为了测试方便，暂且设置1分钟)
                            mp.setExpiration(env.getProperty("mq.kill.item.success.kill.expire"));
                            return message;
                        }
                    });
                }
            }
        }catch (Exception e){
            log.error("确认订单成功后生成订单-发送信息入死信队列，等待着一定时间失效超时未支付的订单-发生异常，消息为：{}",oNo,e.fillInStackTrace());
        }
    }


}




























